import * as PIXI from 'pixi.js';
import { GameConfig } from '../config';

export class Player {
    public container: PIXI.Container;
    private config: GameConfig;
    private character: PIXI.Graphics;
    private shadow: PIXI.Graphics;
    private movement: 'none' | 'left' | 'right' = 'none';
    private position: number;  // 横向位置
    private collisionEffect: PIXI.Container | null = null;
    private velocity: number = 0;
    private readonly acceleration: number = 1000;  // 加速度
    private readonly maxVelocity: number = 400;   // 最大速度
    private readonly friction: number = 800;      // 摩擦力/减速度

    constructor(config: GameConfig) {
        this.config = config;
        this.container = new PIXI.Container();
        this.position = config.screenWidth / 2;
        
        // 创建角色
        this.character = this.createCharacter();
        this.shadow = this.createShadow();
        
        // 添加到容器
        this.container.addChild(this.character);
        this.container.addChild(this.shadow);
        
        // 设置初始位置
        this.updatePosition();
    }

    private createCharacter(): PIXI.Graphics {
        const graphics = new PIXI.Graphics();
        
        // 只绘制人形标记，不绘制背景框
        graphics.lineStyle(2, 0x2c3e50);  // 深色线条
        
        // 头部（圆形）
        graphics.beginFill(0xffffff);
        graphics.drawCircle(
            0, 
            -this.config.player.height * 0.75,
            this.config.player.width * 0.2
        );
        graphics.endFill();
        
        // 身体（线条）
        graphics.moveTo(0, -this.config.player.height * 0.6);
        graphics.lineTo(0, -this.config.player.height * 0.3);
        
        // 手臂
        graphics.moveTo(-this.config.player.width * 0.3, -this.config.player.height * 0.5);
        graphics.lineTo(this.config.player.width * 0.3, -this.config.player.height * 0.5);
        
        // 腿
        graphics.moveTo(0, -this.config.player.height * 0.3);
        graphics.lineTo(-this.config.player.width * 0.2, -this.config.player.height * 0.1);
        graphics.moveTo(0, -this.config.player.height * 0.3);
        graphics.lineTo(this.config.player.width * 0.2, -this.config.player.height * 0.1);
        
        // 设置位置（在天桥上）
        graphics.y = this.config.screenHeight * 0.55;
        
        return graphics;
    }

    private createShadow(): PIXI.Graphics {
        const graphics = new PIXI.Graphics();
        
        // 影子的基本尺寸（基于人物尺寸）
        const width = this.config.player.width * 1.2;  // 略微加宽影子
        const height = width * 2;  // 保持1:2的比例
        const alpha = 0.3;  // 影子透明度
        const skewX = 0.3;  // 向右倾斜的角度
        
        graphics.lineStyle(1, 0x2c3e50, alpha);  // 边框
        graphics.beginFill(0x34495e, alpha);     // 填充
        
        // 计算倾斜后的坐标偏移
        const getSkewedX = (x: number, y: number) => x + y * skewX;
        
        // 绘制人形影子
        // 头部（拉长的椭圆）
        const headWidth = width * 0.4;
        const headHeight = height * 0.15;
        const headY = -height * 0.9;
        graphics.drawEllipse(getSkewedX(0, headY), headY, headWidth / 2, headHeight / 2);
        
        // 身体（拉长的梯形）
        const shoulderY = -height * 0.8;
        graphics.moveTo(getSkewedX(-width * 0.3, shoulderY), shoulderY);  // 肩部左侧
        graphics.lineTo(getSkewedX(width * 0.3, shoulderY), shoulderY);   // 肩部右侧
        graphics.lineTo(getSkewedX(width * 0.4, 0), 0);                   // 底部右侧
        graphics.lineTo(getSkewedX(-width * 0.4, 0), 0);                  // 底部左侧
        graphics.lineTo(getSkewedX(-width * 0.3, shoulderY), shoulderY);  // 回到肩部左侧
        
        // 手臂（拉长的线条）
        // 左手
        const leftArmY = -height * 0.75;
        const leftHandY = -height * 0.5;
        graphics.moveTo(getSkewedX(-width * 0.3, leftArmY), leftArmY);
        graphics.lineTo(getSkewedX(-width * 0.35, leftHandY), leftHandY);
        // 右手
        const rightArmY = -height * 0.75;
        const rightHandY = -height * 0.5;
        graphics.moveTo(getSkewedX(width * 0.3, rightArmY), rightArmY);
        graphics.lineTo(getSkewedX(width * 0.35, rightHandY), rightHandY);
        
        // 腿部（拉长的线条）
        // 左腿
        const legY = -height * 0.3;
        graphics.moveTo(getSkewedX(-width * 0.2, legY), legY);
        graphics.lineTo(getSkewedX(-width * 0.4, 0), 0);
        // 右腿
        graphics.moveTo(getSkewedX(width * 0.2, legY), legY);
        graphics.lineTo(getSkewedX(width * 0.4, 0), 0);
        
        graphics.endFill();
        
        // 设置位置（在马路上）
        graphics.y = this.config.screenHeight * 0.5;
        
        return graphics;
    }

    private updatePosition(): void {
        // 更新角色和影子的横向位置
        this.character.x = this.position;
        this.shadow.x = this.position;
    }

    public update(delta: number): void {
        const dt = delta / 60;  // 转换为秒
        
        // 根据移动方向更新速度
        if (this.movement === 'left') {
            this.velocity = Math.max(
                -this.maxVelocity,
                this.velocity - this.acceleration * dt
            );
        } else if (this.movement === 'right') {
            this.velocity = Math.min(
                this.maxVelocity,
                this.velocity + this.acceleration * dt
            );
        } else {
            // 应用摩擦力减速
            if (this.velocity > 0) {
                this.velocity = Math.max(0, this.velocity - this.friction * dt);
            } else if (this.velocity < 0) {
                this.velocity = Math.min(0, this.velocity + this.friction * dt);
            }
        }
        
        // 更新位置
        this.position = Math.max(
            this.config.player.width / 2,
            Math.min(
                this.config.screenWidth - this.config.player.width / 2,
                this.position + this.velocity * dt
            )
        );
        
        // 更新显示位置
        this.updatePosition();
    }

    public startMoving(direction: 'left' | 'right'): void {
        this.movement = direction;
    }

    public stopMoving(): void {
        this.movement = 'none';
    }

    public getShadowPolygon(): PIXI.Rectangle {
        const width = this.config.player.width * 1.2;
        const height = width * 2;  // 保持1:2的比例
        const skewX = 0.3;  // 保持与影子绘制相同的倾斜角度
        
        // 返回矩形碰撞区域
        return new PIXI.Rectangle(
            this.shadow.x - width * 0.4,
            this.shadow.y - height,
            width * 0.8,  // 使用稍微窄一点的碰撞区域
            height
        );
    }

    public updateCollisionBox(): void {
        // 只更新碰撞检测数据，不再显示碰撞框
        this.getShadowPolygon();
    }

    public showCollisionEffect(collisionPoint: PIXI.Point): void {
        // 移除旧的碰撞效果（如果有）
        if (this.collisionEffect) {
            this.container.removeChild(this.collisionEffect);
        }

        // 创建碰撞效果容器
        this.collisionEffect = new PIXI.Container();
        this.collisionEffect.position.set(collisionPoint.x, collisionPoint.y);

        // 创建爆炸效果
        const explosion = new PIXI.Graphics();
        
        // 绘制爆炸形状
        for (let i = 0; i < 8; i++) {
            const angle = (i / 8) * Math.PI * 2;
            const length = 15 + Math.random() * 10;
            
            explosion.lineStyle(2, 0xff3300);
            explosion.moveTo(0, 0);
            explosion.lineTo(
                Math.cos(angle) * length,
                Math.sin(angle) * length
            );
        }

        // 添加中心圆
        explosion.beginFill(0xff3300);
        explosion.drawCircle(0, 0, 5);
        explosion.endFill();

        this.collisionEffect.addChild(explosion);
        this.container.addChild(this.collisionEffect);

        // 动画效果
        let scale = 1;
        let alpha = 1;
        const animate = () => {
            if (!this.collisionEffect) return;
            
            scale += 0.1;
            alpha -= 0.05;
            
            this.collisionEffect.scale.set(scale);
            this.collisionEffect.alpha = alpha;

            if (alpha > 0) {
                requestAnimationFrame(animate);
            } else {
                this.container.removeChild(this.collisionEffect);
                this.collisionEffect = null;
            }
        };

        animate();
    }

    public reset(): void {
        this.position = this.config.screenWidth / 2;
        this.movement = 'none';
        this.velocity = 0;  // 重置速度
        this.updatePosition();
        
        if (this.collisionEffect) {
            this.container.removeChild(this.collisionEffect);
            this.collisionEffect = null;
        }
    }
}